package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.CartMapper;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.example.springboot.service.ICartService;
import com.example.springboot.entity.Cart;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgc
 * @since 2023-09-14
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private ICartService cartService;

    @Resource
    private CartMapper cartMapper;

    //新增和修改
    @HoneyLogs(operation = "购物车",type = LogType.UPDATE)
    @PostMapping
    public Result save(@RequestBody Cart cart) {  //@RequestBody接收前端数据
        Integer userId = TokenUtils.getCurrentUser().getId();

        // 更新购物车里面相同的商品的数量
        Integer goodsId = cart.getGoodsId();

        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("goods_id", goodsId);
        Cart db = cartService.getOne(queryWrapper);
        if (db != null) {
            db.setNum(db.getNum() + cart.getNum());
            cartService.updateById(db);
            return Result.success();
        }

        // 新增或更新
        if (cart.getId() == null) {
            cart.setTime(DateUtil.now());
            cart.setUserId(userId);
        }
        cartService.saveOrUpdate(cart);
        return Result.success();
    }

//    直接购物车商品，也就是那个+ 按钮
    @PostMapping("/num/{id}/{num}")
    public Result updateNum(@PathVariable Integer id, @PathVariable Integer num) {
        cartMapper.updateNum(num, id);
        return Result.success();
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(cartService.list());
    }

    //删除
    @HoneyLogs(operation = "购物车",type = LogType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(cartService.removeById(id));
    }

    //批量删除
    @HoneyLogs(operation = "购物车",type = LogType.BATCH_DELETE)
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(cartService.removeByIds(ids));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(cartService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
//
//        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        if (!"".equals(name)) {
//            queryWrapper.like("name", name);
//        }
        User currentUser = TokenUtils.getCurrentUser();
        Integer userId = currentUser.getId();
        String role = currentUser.getRole();

        // name是商品的名称
        return Result.success(cartMapper.CartPage(new Page<>(pageNum, pageSize), userId, role, name));
    }
}



